package com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.persenter;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.bean.DevicesUndeMettingBean;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.bean.SetCurrentMeetingBean;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.config.RBConfig;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.contract.ISetCurrentMeetingContract;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.modle.SetCurrentMeetingModle;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.JsonUitl;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/8/13.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class SetCurrentMeetingPersenter implements ISetCurrentMeetingContract.ISetCurrentMeetingPersenter {
    private ISetCurrentMeetingContract.ISetCurrentMeetingView mISetCurrentMeetingView;
    private String TAG = SetCurrentMeetingPersenter.class.getSimpleName();
    private ISetCurrentMeetingContract.ISetCurrentMeetingModle mISetCurrentMeetingModle;
    private List<SetCurrentMeetingBean> mlist;
    private Handler handler = new Handler();

    public SetCurrentMeetingPersenter(ISetCurrentMeetingContract.ISetCurrentMeetingView mISetCurrentMeetingView) {
        this.mISetCurrentMeetingView = mISetCurrentMeetingView;
        mISetCurrentMeetingModle = new SetCurrentMeetingModle();
    }

    @Override
    public void selectorWhetherPublic(SetCurrentMeetingBean bean,Context context) {
       int mettshare =  bean.getMeetShare();
       int mymettshare;
       if(mettshare == 20){
           mymettshare = 10;
           // todo 存储数据
       }else{
           mymettshare = 20;
           // todo 存储数据
       }
       submitSetShare(bean,mymettshare,context);

    }



    @Override
    public void initTitle() {
        mISetCurrentMeetingView.showTitle();
    }

    @Override
    public void LoadDate(Context context, String id) {
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.METTING_INFO)
                .addHeader("sessionId",mISetCurrentMeetingModle.getToken(context))
                .addParams("id", id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        //Log.e(TAG, "loadRecyclerViewDate:token ==sdg111===> "+ response.body().string());
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                if(codo == 100){
                                    String normalDate = JsonUitl.getStringDateFeld(object);
                                    Type type = new TypeToken<List<SetCurrentMeetingBean>>(){}.getType();
                                    mlist = new Gson().fromJson(normalDate,type);
                                    if(mlist != null && mlist.size()>0){
                                        SetCurrentMeetingBean bean =    mlist.get(0);
                                        mISetCurrentMeetingView.setMettingInfo(bean);
                                    }
                                }
                            }
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    @Override
    public void changeName(Context context,SetCurrentMeetingBean bean) {
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.SET_METTING_SHARE)
                .addHeader("sessionId",mISetCurrentMeetingModle.getToken(context))
                .addParams("id", bean.getId())
                .addParams("name", bean.getName())
                .addParams("meetShare", bean.getMeetShare()+"")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                if(codo == 100){
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            mISetCurrentMeetingView.showToastMessage("修改会议名称成功！");
                                        }
                                    });
                                }else{
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            mISetCurrentMeetingView.showToastMessage("修改会议名称失败！");
                                        }
                                    });

                                }
                            }
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                mISetCurrentMeetingView.showToastMessage("修改会议名称失败！");
                            }
                        });
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    public void submitSetShare(final SetCurrentMeetingBean bean, final int mymettshare, Context context) {
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.SET_METTING_SHARE)
                .addHeader("sessionId",mISetCurrentMeetingModle.getToken(context))
                .addParams("id", bean.getId())
                .addParams("name", bean.getName())
                .addParams("meetShare", mymettshare+"")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                       // Log.e(TAG, "loadRecyclerViewDate:token ==sdg3333===> "+ response.body().string());
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                if(codo == 100){
                                   // bean.setMeetShare(20);

                                    showSucessInfo(mymettshare,bean);

                                }else{
                                    showFailureInfo(mymettshare,bean);

                                }
                            }
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        showFailureInfo(mymettshare,bean);
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
    /**
     * @desc 修改成功提示
     * */
    private void showSucessInfo(final int mymettshare, final SetCurrentMeetingBean bean) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if(mymettshare == 20){  // 想关闭
                    bean.setMeetShare(20);
                    // todo 已经公开现在是想关闭公开 成功了
                    mISetCurrentMeetingView.showWhether();
                    // todo toast 提示
                    mISetCurrentMeetingView.showToastMessage("该会议被禁止公开了！");
                }else{  // 想公开
                    bean.setMeetShare(10);
                    // todo 未公开现在想打开公开 成功了
                    mISetCurrentMeetingView.showPublic();
                    mISetCurrentMeetingView.showToastMessage("该会议被公开了！");
                }
            }
        });

    }
    /**
     * @desc 修改失败提示
     * */
    private void showFailureInfo(final int mymettshare, final SetCurrentMeetingBean bean) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if(mymettshare == 20){  // 想关闭
                    bean.setMeetShare(10);
                    // todo 已经公开现在是想关闭公开但是失败了
                    mISetCurrentMeetingView.showPublic();
                    mISetCurrentMeetingView.showToastMessage("未能禁止公开会议！");
                }else{  // 想公开
                    bean.setMeetShare(20);
                    // todo 未公开现在想打开公开但是打开失败了
                    mISetCurrentMeetingView.showWhether();
                    mISetCurrentMeetingView.showToastMessage("未能公开会议！");
                }
            }
        });
    }

}
